<?php
namespace Weixin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->validate();
        if ($echostr = I('get.echostr')) {
            //第一次验证接口
            echo $echostr;
        }else{
            //进行消息处理
            $this->responeMsg();
        }
    }
    //验证此消息是否来自微信
    private function validate(){
        //获得参数 signature nonce timestamp echostr 和本地参数token
        $signature      = I('get.signature');
        $nonce          = I('get.nonce');
        $timestamp      = I('get.timestamp');
        $token          = C('TOKEN');
        $array  = array($nonce,$timestamp,$token);
        sort($array);
        $str    = sha1(implode('',$array));
        if ($str == $signature) {
            return;
        }else{
            exit;
        }
    }

    //处理消息
    private function responeMsg()
    {
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string( $postArr );
        //判断该数据包是否是订阅的事件推送
        if( strtolower( $postObj->MsgType) == 'event'){
            //如果是关注 subscribe 事件
            if( strtolower($postObj->Event) == 'subscribe' ){
                //回复用户消息(纯文本格式) 
                $this->subscribeEvent($postObj);
            }
        }
        elseif(strtolower( $postObj->MsgType) == 'text'){
            //分析关键字，返回对应内容
            $this->textType($postObj);
        }
    }

    //处理关注事件
    private function subscribeEvent($postObj)
    {
        //回复用户消息(纯文本格式) 
        $toUser   = $postObj->FromUserName;
        $fromUser = $postObj->ToUserName;
        $time     = time();
        $msgType  =  'text';
        $content  = 'http://0273.jiaoyinet.com/';
        $template = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    </xml>";
        $info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
        echo $info;

    }

    //获取用户基本信息
    private function getUserInfo($openid)
    {
        $accessToken = D('AccessToken');
        $token = $accessToken->findToken();
        if ($token) {
            print_r($token);
        }
    }

    //处理文字消息
    private function textType($postObj)
    {
        if (trim($postObj->Content)=='openid') {
            //回复用户消息(纯文本格式) 
            $toUser   = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time     = time();
            $msgType  =  'text';
            $content  = $postObj->FromUserName;
            $template = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
            $info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
        }
        else{
            //回复用户消息(纯文本格式) 
            $toUser   = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time     = time();
            $msgType  =  'text';
            $content  = 'http://0273.jiaoyinet.com/';
            $template = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
            $info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
        }
    }
}